import { ref } from 'vue';
import { cancelReconciliationApi } from '@/api/sj/purchaseManagement/toBeStored/index'
import { purchaseCancelReconciliationApi } from '@/api/sj/purchaseManagement/purchaseOrder/index'
import { machiningCancelReconciliationApi } from '@/api/sj/purchaseManagement/outsourcingProcessing/index';

const useApplyForReconciliation = () => {


    const applyVisible = ref(false);
    const applyObj = ref<any>();

    /**
     * @description: 打开申请对账的弹窗
     */
    const handleOpenApply = (row: any) => {
        console.log(row);
        applyObj.value = row
        applyVisible.value = true;
    };


    /**
     * @description: 撤销申请对账
     */
    const revokeOpenApply = (row: any, fn: Function) => {
        // 该订单尚未对账，不能撒销！
        if (row.reconciliationFlag == '2') {
            ElMessageBox.alert('该订单已经付款成功，不能撒销！', '提示', {
                confirmButtonText: '确定',
                dangerouslyUseHTMLString: true,
                type: 'warning'
            })
                .then(() => {
                })
                .catch(() => { });
        } else if (row.reconciliationFlag == '0') {
            ElMessageBox.alert('该订单未对账，不能撒销！', '提示', {
                confirmButtonText: '确定',
                dangerouslyUseHTMLString: true,
                type: 'warning'
            })
                .then(() => {
                })
                .catch(() => { });
        } else {
            ElMessageBox.confirm('确定要撒销该订单的对账吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    try {
                        const { code } = await cancelReconciliationApi({ id: row.id })
                        if (code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '撤销成功!'
                            });

                            fn();
                        }
                    } catch (error) {

                    }
                })
                .catch(() => { });
        }

    };

    /**
     * @description: 已采购列表 撤销对账
     */
    const handleRevokeApply = (row: any, fn: Function) => {

        if (row.reconciliationFlag == '2') {
            ElMessageBox.alert('该订单已经付款成功，不能撒销！', '提示', {
                confirmButtonText: '确定',
                dangerouslyUseHTMLString: true,
                type: 'warning'
            })
                .then(() => {
                })
                .catch(() => { });
        } else if (row.reconciliationFlag == '0') {
            ElMessageBox.alert('该订单未对账，不能撒销！', '提示', {
                confirmButtonText: '确定',
                dangerouslyUseHTMLString: true,
                type: 'warning'
            })
                .then(() => {
                })
                .catch(() => { });
        } else {
            ElMessageBox.confirm('确定要撒销该订单的对账吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    try {
                        const { code } = await purchaseCancelReconciliationApi({ id: row.purchaseInRecordId })
                        if (code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '撤销成功!'
                            });

                            fn();
                        }
                    } catch (error) {

                    }
                })
                .catch(() => { });
        }

    };

      /**
     * @description: 外协加工单 撤销对账
     */
      const cancellationOfOutsourcing = (row: any, fn: Function) => {

        if (row.reconciliationFlag == '2') {
            ElMessageBox.alert('该订单已经付款成功，不能撒销！', '提示', {
                confirmButtonText: '确定',
                dangerouslyUseHTMLString: true,
                type: 'warning'
            })
                .then(() => {
                })
                .catch(() => { });
        } else if (row.reconciliationFlag == '0') {
            ElMessageBox.alert('该订单未对账，不能撒销！', '提示', {
                confirmButtonText: '确定',
                dangerouslyUseHTMLString: true,
                type: 'warning'
            })
                .then(() => {
                })
                .catch(() => { });
        } else {
            ElMessageBox.confirm('确定要撒销该订单的对账吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    try {
                        const { code } = await machiningCancelReconciliationApi({ id: row.id })
                        if (code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '撤销成功!'
                            });

                            fn();
                        }
                    } catch (error) {

                    }
                })
                .catch(() => { });
        }

    };

    return {
        applyVisible,
        applyObj,
        handleOpenApply,
        revokeOpenApply,
        cancellationOfOutsourcing,
        handleRevokeApply
    }
};

export default useApplyForReconciliation;